package ij.hkust.QuickPALM;

import ij.IJ;
import ij.gui.NonBlockingGenericDialog;
import ij.plugin.PlugIn;

import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import QuickPALM.AOTFController;

/**
 * 
 * @author linshan
 *
 */
public class AOTFTest implements PlugIn
{
	
	AOTFController aotf;
	MyDialogs dg = new MyDialogs();
		
	public void run(String arg)
	{
		dg.run = true;
		
		aotf = new AOTFController();		
						
		if (aotf.Open())
		{
			IJ.showMessage("AOTF loaded");
			
			
			aotf.Write("dau dis\r");
			aotf.Write("dds a 0 0\r");
			aotf.Write("dds a 1 0\r");
			aotf.Write("dds a 2 0\r");
			aotf.Write("dds a 3 0\r");
			aotf.Write("dds a 4 0\r");
			aotf.Write("dds a 5 0\r");
			aotf.Write("dds a 6 0\r");
			aotf.Write("dds a 7 0\r");
			
			aotf.Write("dds f 0 153.7560\r");
			aotf.Write("dds f 1 123.1750\r");
			aotf.Write("dds f 2 113.7680\r");
			aotf.Write("dds f 3 99.2574\r");
			aotf.Write("dds f 4 93.6800\r");
			aotf.Write("dds f 5 77.3082\r");
			aotf.Write("dds f 6 0\r");
			aotf.Write("dds f 7 0\r");
			
			IJ.showMessage("AOTF initialized. Pulse testing");												
		    
			aotf.Write("dau en\r");
			aotf.Write("dau gain * 255\r");					
			
			test_stop stopper = new test_stop();	
			stopper.mysetup(dg);
			stopper.start();//to stop the while loop below
						
			
			while (dg.run)
			{				
				aotf.Write("dds a 5 "+dg.power_656+"\r");
				aotf.Write("dds a 2 "+dg.power_488+"\r");
				//modified on 2013.4.24 for PALM (original: "dds a 4 "+dg.power_561+"\r" )
				
				aotf.Write("dds a 0 "+dg.power_405+"\r");								
				
				try {
					Thread.sleep(dg.pulse_duration);
				} catch (InterruptedException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
								
				aotf.Write("dds a 0 0\r");								
				
				try {
					Thread.sleep(dg.pulse_separation);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				/*aotf.Write("dds a 2 "+dg.power_488+"\r");
				
				try {
					Thread.sleep(dg.pulse_duration);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				aotf.Write("dds a 2 0\r");
				
				try {
					Thread.sleep(dg.pulse_separation);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
			
				// Above code is deactivated on 2013/4/24 for PALM testing					
			
			}
			
			if (! dg.pulse_stop)
			{
			   aotf.Write("dau dis\r");
			}
			
			
			if (aotf.Close())
				IJ.showMessage("AOTF closed");
		}
		else
		{
			IJ.showMessage("Can not load AOTF");
		}
	}
}


class test_stop extends Thread //multi-thread for killing the loop
{
	private MyDialogs dg;
	NonBlockingGenericDialog stop;
	Button pulse_settings = new Button ("Settings");
	ij.Prefs prefs = new ij.Prefs();

	public void mysetup(MyDialogs dg)
	{
		this.dg = dg;		
	}

	public void run()
	{		
		stop = new NonBlockingGenericDialog("");
		stop.hideCancelButton();
		stop.setOKLabel("Stop");		
		stop.add(pulse_settings);		
		stop.addMessage("");
		stop.addCheckbox("Stop Pulses Only", false);
		pulse_settings.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				test_setting setting = new test_setting();				
				setting.mysetup(dg);
				setting.start();
			}
		});
		
		stop.showDialog();		
				
		this.dg.pulse_stop = stop.getNextBoolean();
		
		if (stop.wasOKed())			
			prefs.set("QuickPALM.pulse_duration", (double) this.dg.pulse_duration);
		    prefs.set("QuickPALM.pulse_separation", (double) this.dg.pulse_separation);		
		    prefs.set("QuickPALM.power_656", this.dg.power_656);
		    prefs.set("QuickPALM.power_561", this.dg.power_561);
		    prefs.set("QuickPALM.power_488", this.dg.power_488);
		    prefs.set("QuickPALM.power_405", this.dg.power_405);
		this.dg.run = false;
	}
	
}


class StopMulticolor extends Thread //multi-thread for killing the loop
{
	private MyDialogs dg;
	NonBlockingGenericDialog stop;
	Button pulse_settings = new Button ("Settings");
	ij.Prefs prefs = new ij.Prefs();

	public void mysetup(MyDialogs dg)
	{
		this.dg = dg;		
	}

	public void run()
	{		
		stop = new NonBlockingGenericDialog("");
		stop.hideCancelButton();
		stop.setOKLabel("Stop");		
		stop.add(pulse_settings);		
		stop.addMessage("");
		pulse_settings.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				test_setting setting = new test_setting();				
				setting.mysetup(dg);
				setting.start();
			}
		});
		
		stop.showDialog();		
						
		if (stop.wasOKed())			
			prefs.set("QuickPALM.pulse_duration", (double) this.dg.pulse_duration);
		    prefs.set("QuickPALM.pulse_separation", (double) this.dg.pulse_separation);		
		    prefs.set("QuickPALM.power_656", this.dg.power_656);
		    prefs.set("QuickPALM.power_561", this.dg.power_561);
		    prefs.set("QuickPALM.power_488", this.dg.power_488);
		    prefs.set("QuickPALM.power_405", this.dg.power_405);
		this.dg.run = false;
	}
	
}


class test_setting extends Thread
{
	private MyDialogs dg;
	NonBlockingGenericDialog setting;
	
	public void mysetup(MyDialogs dg)
	{
		this.dg = dg;
	}
	
	public void run()
	{		
		setting = new NonBlockingGenericDialog("Settings");
		setting.hideCancelButton();		
		setting.addNumericField("Pulse Duration in MS", (double)dg.pulse_duration, 0);
		setting.addNumericField("Pulse Separation in MS", (double)dg.pulse_separation, 0);
		setting.addMessage("");
		setting.addStringField("656nm Power (0~5500)", this.dg.power_656);
		setting.addStringField("561nm Power (0~5500)", this.dg.power_561);
		setting.addStringField("488nm Power (0~5500)", this.dg.power_488);
		setting.addStringField("405nm Power (0~5500)", this.dg.power_405);
		setting.showDialog();		
		
		this.dg.pulse_duration = (long) setting.getNextNumber();
		this.dg.pulse_separation = (long) setting.getNextNumber();
		this.dg.power_656 = setting.getNextString();
		this.dg.power_561 = setting.getNextString();
		this.dg.power_488 = setting.getNextString();
		this.dg.power_405 = setting.getNextString();
		
	}
}
